﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DblogStudio.BLL;
using DblogStudio.DAL.Models;
using DblogStudio.GUI.Common;
using DblogStudio.GUI.Utils;
using DblogStudio.Utils;

namespace DblogStudio.GUI.DuLieu
{
    public partial class FormQuanLyNhiemVuBaoTri_ChiTiet : BaseDialog
    {
        NhiemVuBaoTri nhiemVu;
        public event Common.TListChangedEventHandler OnChanged;

        public FormQuanLyNhiemVuBaoTri_ChiTiet(NhiemVuBaoTri nhiemVu = null)
        {
            InitializeComponent();
            this.nhiemVu = nhiemVu;
            InitInterface();
        }

        void InitInterface()
        {
            if (nhiemVu != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.NHIEMVUBAOTRI);
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
                txtTen.Text = nhiemVu.TenNhiemVuBaoTri;
                txtGhiChu.Text = nhiemVu.GhiChu;
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.NHIEMVUBAOTRI);
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
                txtTen.Text = "";
                txtGhiChu.Text = "";
            }
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            
            if (txtTen.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_FIELD_EMPTY, ENTITY_NAME.NHIEMVUBAOTRI));
                txtTen.Focus();
                return false;
            }
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (nhiemVu != null)
                {
                    #region Cập nhật thông tin 
                    nhiemVu.TenNhiemVuBaoTri = txtTen.Text;
                    nhiemVu.GhiChu = txtGhiChu.Text;
                    var rs = BizNhiemVuBaoTri.Update(nhiemVu);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.NHIEMVUBAOTRI));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.NHIEMVUBAOTRI));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới 
                    nhiemVu = new NhiemVuBaoTri();
                    nhiemVu.TenNhiemVuBaoTri = txtTen.Text;
                    nhiemVu.GhiChu = txtGhiChu.Text;
                    var rs = BizNhiemVuBaoTri.Add(nhiemVu);
                    nhiemVu= null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        OnChanged(this, null);
                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                            txtTen.Focus();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTen, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.NHIEMVUBAOTRI));
                        txtTen.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.NHIEMVUBAOTRI));
                        return;
                    }
                    #endregion
                }
            }
        }

    }
}
